<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

class ot_subtotal {

	function ot_subtotal() {
		$this->module_key = 'MODULE_ORDER_TOTAL_SUBTOTAL';
		$this->code = 'ot_subtotal';
		$this->enabled = ((MODULE_ORDER_TOTAL_SUBTOTAL_STATUS == 'true') ? true : false);
		$this->sort_order = MODULE_ORDER_TOTAL_SUBTOTAL_SORT_ORDER;
	}

	function process($params, &$checkout_component)
	{
		$cart = &$checkout_component->cart;
		$cart_prices = &$checkout_component->cart_prices;

		$cartSubtotalPrice = 0.0;
		$sz = sizeof($cart);
		for ($i=0; $i< $sz; $i++)
		{
			$price = (double)$cart[$i]['price'];
			$specialPrice = (double)$cart[$i]['special_price'];
			if ($specialPrice > 0.00000001 && $price > $specialPrice) $price = $specialPrice;
	
			$discountQuantity = $this->getDiscountCategoryQuantities($cart, (int)$cart[$i]['discount_categories_id']);
			if ($discountQuantity < 1) $discountQuantity = $cart[$i]['cart_qty'];
	
			$discountPrice = $this->getDiscountPrice($cart[$i]['price_breaks'], $discountQuantity);
			if ($discountPrice > 0.00000001 && $price > $discountPrice) $price = $discountPrice;

			$cart[$i]['products_price'] = $price;

			$attributes = $cart[$i]['cart_options'];
			$sz1 = sizeof($attributes);
			for ($j=0; $j<$sz1; $j++)
			{
				$prefix = $attributes[$j]['values_price_prefix'];
				if ($prefix == '+') {
					$price = $price + $attributes[$j]['values_price'];
				}
				else if ($prefix == '-') {
					$price = $price - $attributes[$j]['values_price'];
				}
			}

			$cart[$i]['cart_price'] = $price;
			$cartSubtotalPrice += ($price * (int)$cart[$i]['cart_qty']);
		}

		$cart_prices[$this->code] = array('value' => $cartSubtotalPrice, 'title' => 'Sub-Total:', 'class' => $this->code);
	}

	function getDiscountCategoryQuantities(&$cart, $category)
	{
		$quantity = 0;
		if ($category > 0)
		{
			$sz = sizeof($cart);
			for ($i=0; $i< $sz; $i++)
			{
				if ($category == $cart[$i]['discount_categories_id'])
				{
					$quantity = $quantity + $cart[$i]['cart_qty'];
				}
			}
		}
		return (int)$quantity;
	}

	function getDiscountPrice($price_breaks, $quantity) 
	{
		$price = 0.0;
		$matchingQuantity = 0;
		if ($price_breaks != null)
		{
			$sz = sizeof($price_breaks);
			for ($i=0; $i< $sz; $i++)
			{
				$pb_quantity = $price_breaks[$i]['quantity'];
				if ($quantity >= $pb_quantity)
				{
					if ($pb_quantity > $matchingQuantity)
					{
						$matchingQuantity = $pb_quantity;
						$price = $price_breaks[$i]['price'];
					}
				}
			}
		}
		return $price;
	}

	function check() {
		if (!isset($this->_check)) {
			$this->_check = check_module($this->module_key);
		}
		return $this->_check;
	}

	function install() {
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Display Coupon', 'MODULE_ORDER_TOTAL_SUBTOTAL_STATUS', 'true', 'Do you want to display the subtotal?', '6', '1','true, false', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_SUBTOTAL_SORT_ORDER', '10', 'Sort order of display.', '6', '2', now())");
	}

	function remove() {
		remove_module($this->module_key);
	}
}
?>
